package ru.spbau.lifeincube.math;

public class Quaternion {
	private double[] data;

	public Quaternion() {
		data = new double[4];
		data[3] = 1.0;
	}

	public Quaternion(double[] a) {
		data = new double[4];
		for (int i = 0; i < 4; i++) {
			data[i] = a[i];
		}
	}

	public double getX() {
		return data[0];
	}

	public double getY() {
		return data[1];
	}

	public double getZ() {
		return data[2];
	}

	public double getW() {
		return data[3];
	}

	public void setX(double val) {
		data[0] = val;
	}

	public void setY(double val) {
		data[1] = val;
	}

	public void setZ(double val) {
		data[2] = val;
	}

	public void setW(double val) {
		data[3] = val;
	}

	public void normalize() {
		double len = length();
		if (len <= 1e-5) {
			data = new double[] {0.0, 0.0, 0.0, 1.0};
			return;
		}
		data = new double[]{data[0] / len, data[1] / len, data[2] / len, data[3] / len};
	}
	
	public double length() {
		return Math.sqrt(data[0] * data[0] + data[1] * data[1] + data[2] * data[2] + data[3] * data[3]);		
	}
}
